﻿Public Class xtf_ABMarticulo2

    Dim ulg_row As DataRow
    Dim obj_Articulo As cls_articulo_ = Nothing

    Public Sub New()
        InitializeComponent()
        Cargararticulorubro_()
        Cargarunidad_()
        pic_imagen.Tag = ""
        AgregarValidaciones()
    End Sub

    Public Overrides Sub CargarObjetos()
        Cargararticulorubro_()
        Cargarunidad_()
    End Sub

    Private Sub Cargarunidad_()
        Dim tmp_obj As Object = Nothing
        If Not IsNothing(cbo_Unidad.SelectedItem) Then
            tmp_obj = cbo_Unidad.SelectedItem
        End If
        cbo_Unidad.Properties.Items.Clear()

        Dim unidad_ As New cls_unidad_
        If unidad_.CargarTodos Then
            For Each unidad As cls_unidad_ In unidad_.Items
                cbo_Unidad.Properties.Items.Add(unidad)
            Next
            cbo_Unidad.SelectedIndex = 0
            cbo_Unidad.Enabled = True

            If Not IsNothing(tmp_obj) Then
                For Each obj As cls_unidad_ In cbo_Unidad.Properties.Items
                    If obj.idunidad = tmp_obj.idunidad Then
                        cbo_Unidad.SelectedItem = obj
                        Exit For
                    End If
                Next
            End If
        Else
            cbo_Unidad.Enabled = False
            cbo_Unidad.SelectedItem = Nothing
        End If
    End Sub

    Private Sub Cargararticulorubro_()
        Dim tmp_obj As Object = Nothing
        If Not IsNothing(cbo_articulorubro.SelectedItem) Then
            tmp_obj = cbo_articulorubro.SelectedItem
        End If

        cbo_articulorubro.Properties.Items.Clear()
        Dim articulorubro_ As New cls_articulorubro_
        If articulorubro_.CargarTodos Then
            For Each articulorubro As cls_articulorubro_ In articulorubro_.Items
                cbo_articulorubro.Properties.Items.Add(articulorubro)
            Next
            cbo_articulorubro.SelectedIndex = 0
            cbo_articulorubro.Enabled = True

            If Not IsNothing(tmp_obj) Then
                For Each obj As cls_articulorubro_ In cbo_articulorubro.Properties.Items
                    If obj.idarticulorubro = tmp_obj.idarticulorubro Then
                        cbo_articulorubro.SelectedItem = obj
                        Exit For
                    End If
                Next
            End If
        Else
            cbo_articulorubro.Enabled = False
            cbo_articulorubro.SelectedItem = Nothing
        End If
    End Sub

    Public Shadows Sub Nuevo(ByRef listado As Form)
        Try
            frm_listado = listado
            Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - Nuevo"
            obj_Articulo = New cls_articulo_
            btn_Registro.Enabled = False
            Me.Show()
        Catch ex As Exception
            'Dim frmError As New frm_ErrorManager(ex.Message)
        End Try
    End Sub

    Public Shadows Sub Modificar(ByRef Articulo As cls_articulo_, ByRef listado As Form, ByRef row As DataRow)
        If IsNothing(obj_Articulo) Then
            obj_Articulo = New cls_Articulo_
        End If
        obj_Articulo = Articulo
        frm_listado = listado
        ulg_row = row : bbi_imprimir.Enabled = True

        Me.Text = CType(frm_listado, xtf_ListadoBase).lbl_modulo.Text & " - " & obj_Articulo.ToString
        txt_ID.Text = obj_Articulo.idArticulo
        txt_Nombre.Text = obj_Articulo.nombre
        txt_Codigo.Text = obj_Articulo.codigo
        txt_barcode.Text = obj_Articulo.barcode
        txt_nombrecorto.Text = obj_Articulo.nombrecorto

        Try
            pic_imagen.Image = System.Drawing.Image.FromFile(obj_Articulo.pathimagen)
        Catch ex As Exception
        End Try


        'cbo_articulorubro.Text = obj_Articulo.articulorubro.ToString
        For Each obj As cls_articulorubro_ In cbo_articulorubro.Properties.Items
            If obj.idarticulorubro = obj_Articulo.idarticulorubro Then
                cbo_articulorubro.SelectedItem = obj
                Exit For
            End If
        Next
        txt_Descripcion.Text = obj_Articulo.descripcion
        txt_Notas.Text = obj_Articulo.notas
        cbo_Unidad.Text = obj_Articulo.unidad.ToString
        For Each obj As cls_unidad_ In cbo_Unidad.Properties.Items
            If obj.idunidad = obj_Articulo.idunidad Then
                cbo_Unidad.SelectedItem = obj
                Exit For
            End If
        Next
        If obj_Articulo.iva <> 0 Then
            txt_iva.Text = obj_Articulo.iva
        End If


        'For Each obj As cls_unidaddetalle_ In cbo_unidad.Properties.Items
        '    If obj.idunidaddetalle = obj_Articulo.idunidaddetalle Then
        '        cbo_unidad.SelectedItem = obj
        '        Exit For
        '    End If
        'Next

        btn_Registro.Enabled = True
        obj_Articulo.proveedorArticulo_ = Nothing
        obj_Articulo.articulocomposicionarticulo_ = Nothing
        obj_Articulo.articulocomposicionmaquinariatarea_ = Nothing
        obj_Articulo.articulocomposicionproceso_ = Nothing
        obj_Articulo.articulorubro = Nothing
        obj_Articulo.unidad = Nothing
        obj_Articulo.proveedor = Nothing
        Me.Show()

    End Sub


    Public Overrides Function ComprobarDatos() As Boolean
        If cbo_articulorubro.Enabled = False Then
            ErrManagerShow("001012", Me)
            Return False
        End If
        If cbo_unidad.Enabled = False Then
            ErrManagerShow("001014", Me)
            Return False
        End If
        If Not ControlarValidaciones() Then
            ErrManagerShow("000004", Me)
            Return False
        End If
        Return True
    End Function

    Public Shadows Sub btn_Guardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Guardar.Click
        If ComprobarDatos() Then
            obj_Articulo.nombre = txt_Nombre.Text
            obj_Articulo.nombreventa = txt_Nombre.Text
            obj_Articulo.codigo = txt_Codigo.Text
            obj_Articulo.idarticulorubro = CType(cbo_articulorubro.SelectedItem, cls_articulorubro_).idarticulorubro

            obj_Articulo.descripcion = txt_Descripcion.Text
            obj_Articulo.notas = txt_Notas.Text
            obj_Articulo.enventa = True
            obj_Articulo.tipo = cls_articulo_.enu_tipo.Articulo 
            'obj_Articulo.costo(CDec(CType(cbo_unidaddetalle.SelectedItem, cls_unidaddetalle_).CValGuardar(0)))
            obj_Articulo.tipocosto = cls_articulo_.enu_tipocosto.fijo
            obj_Articulo.pathimagen = pic_imagen.Tag
            obj_Articulo.costobloqueado = True
            obj_Articulo.url = ""
            obj_Articulo.bulto = 0
            obj_Articulo.barcode = txt_barcode.Text
            obj_Articulo.nombrecorto = txt_nombrecorto.Text
            obj_Articulo.iva = txt_iva.Text

            obj_Articulo.idunidad = CType(cbo_unidad.SelectedItem, cls_unidad_).idunidad
            'obj_Articulo.idunidaddetalle = CType(cbo_unidaddetalle.SelectedItem, cls_unidaddetalle_).idunidaddetalle

            If txt_ID.Text = "" Then
                obj_Articulo.Nuevo(True)
                If frm_listado.Visible Then
                    ulg_row = CType(frm_listado, xtf_LISTADOarticulo_2).AgregarItem(obj_Articulo)
                End If
                Modificar(obj_Articulo, frm_listado, ulg_row)
            Else
                obj_Articulo.Modificar()
                If frm_listado.Visible Then
                    CType(frm_listado, xtf_LISTADOarticulo_2).ModificarItem(obj_Articulo, ulg_row)
                End If
                Modificar(obj_Articulo, frm_listado, ulg_row)
            End If
            flg_Modificado = False : btn_Guardar.Enabled = False : bbi_guardar.Enabled = False
        End If
    End Sub

    Private Sub btn_Registro_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Registro.Click
        If Not IsNothing(obj_Articulo.registro) Then
            Dim ABMRegistro As New xtf_ABMRegistro(obj_Articulo.registro)
            If ABMRegistro.ShowDialog() = Windows.Forms.DialogResult.OK Then
                flg_Modificado = True
            End If
        End If
    End Sub


    Private Sub btn_Cerrar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Cerrar.Click
        If flg_Modificado Then
            bol_comprobarCerrado = False
            Select Case ErrManagerShow("000001", Me)
                Case Windows.Forms.DialogResult.Yes
                    If ComprobarDatos() Then
                        btn_Guardar_Click(Nothing, Nothing)
                    Else
                        Exit Sub
                    End If
                Case Windows.Forms.DialogResult.Cancel : bol_comprobarCerrado = True
                    Exit Sub
            End Select
        End If
        Me.Dispose()
    End Sub

    Private Sub pic_imagen_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles pic_imagen.MouseDoubleClick
        Dim OFD As New OpenFileDialog
        OFD.Filter = "Archivos de imagen(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|Todos los archivos (*.*)|*.*"
        If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
            Try
                pic_imagen.Image = System.Drawing.Image.FromFile(OFD.FileName)
                pic_imagen.Tag = OFD.FileName
            Catch ex As Exception
                pic_imagen.Image = Nothing
                pic_imagen.Tag = ""
            End Try
        End If
    End Sub

    'Private Sub cbo_Unidad_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
    '    Dim tmp_obj As Object = Nothing
    '    If Not IsNothing(cbo_unidaddetalle.SelectedItem) Then
    '        tmp_obj = cbo_unidaddetalle.SelectedItem
    '    End If
    '    cbo_unidaddetalle.Properties.Items.Clear()

    '    If Not IsNothing(CType(cbo_Unidad.SelectedItem, cls_unidad_).unidaddetalle_) Then
    '        For Each unidaddetealle As cls_unidaddetalle_ In CType(cbo_Unidad.SelectedItem, cls_unidad_).unidaddetalle_.Items
    '            cbo_unidaddetalle.Properties.Items.Add(unidaddetealle)
    '        Next
    '        cbo_unidaddetalle.SelectedIndex = 0
    '        cbo_unidaddetalle.Enabled = True

    '        If Not IsNothing(tmp_obj) Then
    '            For Each obj As cls_unidaddetalle_ In cbo_unidaddetalle.Properties.Items
    '                If obj.idunidaddetalle = tmp_obj.idunidaddetalle Then
    '                    cbo_unidaddetalle.SelectedItem = obj
    '                    Exit For
    '                End If
    '            Next
    '        End If
    '    Else
    '        cbo_unidaddetalle.Enabled = False
    '        cbo_unidaddetalle.SelectedItem = Nothing
    '    End If
    'End Sub

    Private Sub btn_agregarImagen_Click(sender As System.Object, e As System.EventArgs) Handles btn_agregarImagen.Click
        Dim OFD As New OpenFileDialog
        OFD.Filter = "Archivos de imagen(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|Todos los archivos (*.*)|*.*"
        If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
            Try
                pic_imagen.Image = System.Drawing.Image.FromFile(OFD.FileName)
                pic_imagen.Tag = OFD.FileName
            Catch ex As Exception
                pic_imagen.Image = Nothing
                pic_imagen.Tag = ""
            End Try
        End If
    End Sub

    Private Sub btn_quitarImagen_Click(sender As System.Object, e As System.EventArgs) Handles btn_quitarImagen.Click
        pic_imagen.Image = Nothing
        pic_imagen.Tag = ""
    End Sub

    Private Sub cbo_articulorubro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbo_articulorubro.SelectedIndexChanged
        If Not IsNothing(cbo_articulorubro.EditValue) Then
            txt_iva.Text = cbo_articulorubro.EditValue.iva
        End If
    End Sub

End Class